package me.firstapp.service.weixin.product.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.weixin.product.ProductCategory;
import me.firstapp.repository.weixin.product.ProductCategoryRepository;
import me.firstapp.service.weixin.product.ProductCategoryService;

@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

	@Autowired
	private ProductCategoryRepository productCategoryRepository;

	@Override
	public Page<ProductCategory> findProductCategoryPage(Integer pageNo, Integer pageSize,
			Map<String, Object> searchParams, String sortParam, String sortType) {
		return productCategoryRepository.findProductCategoryPage(pageNo, pageSize, searchParams, sortParam, sortType);
	}

	@Override
	public List<ProductCategory> findProductCategorys() {
		return productCategoryRepository.findProductCategorys();
	}

	@Override
	public ProductCategory executeAdd(String name, String imageUrl, String description, Integer sort, String tag) {
		ProductCategory category = new ProductCategory();
		category.setName(name);
		category.setDescription(description);
		category.setSort(sort);
		category.setStatus(ProductCategory.STATUS_ON);
		category.setTag(tag);
		category.setImageUrl(imageUrl);
		productCategoryRepository.save(category);
		return category;
	}

	@Override
	public ProductCategory executeUpdate(Long id, String name, String imageUrl, String description, Integer sort,
			Integer status, String tag) {
		ProductCategory category = (ProductCategory) productCategoryRepository.getById(id);
		if (StrUtils.isNotNULL(name)) {
			category.setName(name);
		}
		if (StrUtils.isNotNULL(description)) {
			category.setDescription(description);
		}
		if (null != sort) {
			category.setSort(sort);
		}
		if (null != status) {
			category.setStatus(status);
		}
		if (StrUtils.isNotNULL(tag)) {
			category.setTag(tag);
		}
		if (StrUtils.isNotNULL(imageUrl)) {
			category.setImageUrl(imageUrl);
		}
		productCategoryRepository.update(category);
		return category;
	}

	@Override
	public ProductCategory findById(Long id) {
		return (ProductCategory) productCategoryRepository.getById(id);
	}

	@Override
	public List<ProductCategory> findAll() {
		return productCategoryRepository.findAll();
	}

	@Override
	public ProductCategory findByTag(String tag) {
		return productCategoryRepository.findByTag(tag);
	}

}
